package com.autoMobile.servlet;

import com.autoMobile.bean.ddmUser;
import com.autoMobile.dao.ddmUserDao;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




@WebServlet("/ddm/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收用户请求
		HttpSession session=request.getSession();
		request.setCharacterEncoding("UTF-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		//调用dao层login,操作数据库
		ddmUserDao dao=new ddmUserDao();
		ddmUser user=dao.login(username, password);
		//判断并控制转向
		if(user!=null) {
			session.setAttribute("user", user);//将user放在session作用域里
			response.sendRedirect("index.jsp");//重定向到index页面
			
			
		}else {
			session.setAttribute("tishi","用户名或密码错误" );//将信息放到session作用域中
			response.sendRedirect("login.jsp");//重定向的登录界面
		}
		
	}

}
